package dongbai.soft.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import dongbai.soft.dao.UserDao;
import dongbai.soft.entity.UserEntity;
import dongbai.soft.service.UserService;
import dongbai.soft.utils.Constant;
import dongbai.soft.utils.PageUtils;
import dongbai.soft.utils.Query;
import dongbai.soft.utils.TokenGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.TimeUnit;


@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public String SetCookie(String id, String role) {
        String token = TokenGenerator.generateValue();
        redisTemplate.opsForValue().set(Constant.REDIS_TOKEN + token, id, Constant.EXPIRE_TIME, TimeUnit.DAYS);
        return token;
    }


    @Override
    public String getids(String token) {
        return redisTemplate.opsForValue().get(Constant.REDIS_TOKEN + token);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UserEntity> page = this.page(
                new Query<UserEntity>().getPage(params),
                new QueryWrapper<UserEntity>().eq("SID", params.get("SID"))
        );

        return new PageUtils(page);
    }


}